﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using myTwitter.API;
using myTwitter.CustomControls;
using myTwitter.BO;
using myBoard.Framework.BO;
using myBoard.Framework;
using System.Windows.Media.Imaging;
using myTwitter.Model;
using System.Diagnostics;
using myTwitter.DataTemplateSelectors;

namespace myTwitter
{
    public static class GlobalController
    {
        public static class Settings
        {
            private static bool _demoMode = false;
            private static bool _canRefresh = true;
            private static bool _isConnected = false;
            private static bool _showLogs = true;
            private static BO.MagnetSettings _magnetSettings;

            public static BO.MagnetSettings MagnetSettings
            {
                get { return _magnetSettings; }
                set { _magnetSettings = value; }
            }

            public static bool ShowLogs
            {
                get { return _showLogs; }
                set { _showLogs = value; }
            }

            public static bool IsConnected
            {
                get { return _isConnected; }
                set { _isConnected = value; }
            }

            public static bool DemoMode
            {
                get { return _demoMode; }
                set { _demoMode=value; }
            }

            public static bool CanRefresh
            {
                get { return _canRefresh; }
                set { _canRefresh = value; }
            }
        }

        #region Methods

        public static void HideMyBoard()
        {
            Views.Main.HideMyBoard();
        }

        public static void Notify(List<IMessage> messages)
        {
            if (messages.Count == 0)
                return;

            List<Notification> notifications = new List<Notification>(); 
            foreach (IMessage message in messages)
            {
                Notification notification = new Notification();
                notification.CallBackMethod = null;
                notification.Source = "Sinapse";
                notification.Text = message.Text;
                notification.Title = message.User.Name;
                notification.Illustration = new BitmapImage(new Uri(message.User.ProfileImageUrl));
                notifications.Add(notification);
            }

            Connector.Notify(notifications);
        }

        public static void Log(string text)
        {
            if (GlobalController.Settings.ShowLogs)
            {
                Debug.WriteLine(text);
            }
        }

        public static void Notify(IMessage message)
        {
            List<IMessage> messages = new List<IMessage>();
            messages.Add(message);
            Notify(messages);
        }

        public static void SignOut()
        {
            Instances.ITwitterProvider.EndSession();
            Instances.Twitter.Reset();
            Views.Main.SwitchToLoginMode();
        }

        #endregion

        #region Instances

        public static class Instances
        {
            static ITwitterProvider _iTwitterProvider;
            static Singletons _singletons = new Singletons();            

            public static ITwitterProvider ITwitterProvider
            {
                get
                {
                    if (_iTwitterProvider == null)
                        _iTwitterProvider = new Test_TwitterProvider();

                    return _iTwitterProvider;
                }
                set { _iTwitterProvider = value; }
            }

            public static Twitter Twitter
            {
                get
                {
                    return _singletons.Twitter;
                }
            }
        }

        #endregion        

        #region Views

        public static class Views
        {
            static Main _main;
            static IMessagesControl _iMessagesControl;
            static Navigator _navigator;

            public static Navigator Navigator
            {
                get { return _navigator; }
                set { _navigator = value; }
            }

            public static Main Main
            {
                get { return _main; }
                set { _main = value; }
            }
            /*
            public static IMessagesControl IMessagesControl
            {
                get { return _iMessagesControl; }
                set { _iMessagesControl = value; }
            }*/
        }

        #endregion
    }
}
